package cmich.cps.scheduler.algorithms;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import cmich.cps.scheduler.SchedulingSystem;
import cmich.cps.scheduler.data.Course;
import cmich.cps.scheduler.data.Preference;
import cmich.cps.scheduler.data.Professor;
import cmich.cps.scheduler.data.Room;
import cmich.cps.scheduler.data.Schedule;

/**
 * The Class AlgorithmSystem.
 * 
 * @author Jonathan Kissinger <kissi1j@cmich.edu>
 */
public class AlgorithmSystem {
	
	/** The Constant LOGGER. */
	private final static Logger LOGGER = Logger.getLogger(AlgorithmSystem.class.getName());
	
	/** The courses. */
	private List<Course> courses = new ArrayList<Course>();
	
	/** The rooms. */
	private List<Room> rooms = new ArrayList<Room>();
	
	/** The timeslots. */
	private List<String> timeslots = new ArrayList<String>();
	
	/** The professors. */
	private List<Professor> professors = new ArrayList<Professor>();
	
	/** The system. */
	private SchedulingSystem system = SchedulingSystem.getInstance();
	
	/** The master schedule. */
	private Schedule masterSchedule;

	/**
	 * Instantiates a new algorithm system.
	 */
	public AlgorithmSystem() {
		copyVariables();
	}

	/**
	 * Copies all the objects in the scheduling system.
	 */
	private void copyVariables() {
		LOGGER.fine("Copying variables from scheduling system.");
		this.timeslots.addAll(system.getTimeslots());
		this.rooms.addAll(system.getRooms());
		this.professors.addAll(system.getProfessors());
		this.masterSchedule = system.getMasterSchedule();
		this.courses.addAll(system.getCourses());
		for(Professor professor : this.professors) {
			professor.setAssignedCourses(0);
		}
		for(Room room : this.rooms) {
			room.setTimeslots(this.timeslots);
		}
	}
	
	/**
	 * Reset variables.
	 */
	void resetVariables() {
		LOGGER.fine("Resetting algorithm system variables");
		this.courses.clear();
		this.courses.addAll(system.getCourses());
		for(Professor professor : this.professors) {
			professor.setAssignedCourses(0);
		}
		for(Room room : this.rooms) {
			room.setTimeslots(this.timeslots);
		}
	}

	/**
	 * Gets the courses.
	 *
	 * @return the courses
	 */
	List<Course> getCourses() {
		return courses;
	}
	
	/**
	 * Removes the course.
	 *
	 * @param course the course
	 */
	void removeCourse(Course course) {
		this.courses.remove(course);
	}

	/**
	 * Gets the rooms.
	 *
	 * @return the rooms
	 */
	List<Room> getRooms() {
		return rooms;
	}

	/**
	 * Gets the timeslots.
	 *
	 * @return the timeslots
	 */
	List<String> getTimeslots() {
		return timeslots;
	}

	/**
	 * Gets the professors.
	 *
	 * @return the professors
	 */
	List<Professor> getProfessors() {
		return professors;
	}
	
	/**
	 * Gets the object.
	 *
	 * @param name the name
	 * @param objects the objects
	 * @return the object
	 */
	Object getObject(String name, List<?> objects) {
		for(int i=0;i<objects.size();i++) {
			Object object = objects.get(i);
			if(object.toString().equals(name)) {
				return object;
			}
		}
		return null;
	}
	
	/**
	 * Gets the preference value.
	 *
	 * @param professor the professor
	 * @param item the item
	 * @return the preference value
	 */
	Integer getPreferenceValue(Professor professor, String item) {
		for(int i=0;i<professor.getPreferences().size();i++) {
			Preference preference = professor.getPreferences().get(i);
			if(preference.getItem().equals(item)) {
				return preference.getValue();
			}
		}
		return 0;
	}

	/**
	 * Gets the master schedule.
	 *
	 * @return the master schedule
	 */
	Schedule getMasterSchedule() {
		return masterSchedule;
	}
}
